home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / idt.zip / IDT.C < prev    next >
C/C++ Source or Header  |  1988-06-12  |  31KB  |  1,338 lines

  1. #pragma    title("IDT - Find Internal Data Table")
  2. #pragma    subtitle("Introduction")
  3.  
  4. /*
  5. **    program:    idt
  6. **
  7. **    purpose:    This program finds the internal device
  8. **            table used by MS-DOS and reports its
  9. **            contents.  It includes options to dump
  10. **            and/or display each of the various
  11. **            structures pointed to by the idt.
  12. **
  13. **    usage:        idt
  14. **
  15. **    switches:    -i = List the internal data table
  16. **            -p = List the physical device table
  17. **            -l = List the logical device table
  18. **            -d = List the device driver headers
  19. **            -m = List the memory arena
  20. **            -b = List buffer control blocks
  21. **            -f = List internal file control blocks
  22. **            -c = List file control blocks
  23. **            -v = Verbose (dump buffers)
  24. **            -* = Everything (except verbose)
  25. **
  26. **    notes:        Much (most) of the information contained in
  27. **            the idt (including the method used to find
  28. **            it) is undocumented and subject to change
  29. **            and/or errors.  This version will not work
  30. **            for versions of MS-DOS previous to 3.0.
  31. **
  32. **            Considering the wealth of information revealed
  33. **            in these tables, it may me a reasonable risk
  34. **            for an application which takes some care to
  35. **            utilize it - even though it is officially
  36. **            undocumented and will not be supported by
  37. **            the OS vendor (yes, you know who you are!)
  38. **
  39. **    author:        Bill Parrott
  40. **
  41. **            Copyright (c) 1988, Bill Parrott
  42. **             All Rights Reserved
  43. **
  44. **            Use this code in any way and the information
  45. **            contained herein any way you like, without
  46. **            restriction.  If you accomplish something
  47. **            because of what you got here, and if it makes
  48. **            you rich, well I just hope you'll remember
  49. **            who made it all possible :-).  Seriously, a
  50. **            little credit would be nice but apart from
  51. **            that, have at it!
  52. */
  53.  
  54. #define    VERSION        1
  55. #define    REVISION    0
  56.  
  57. #define    PROGRAMNAME    "IDT"
  58.  
  59. /*
  60. **    system includes
  61. */
  62.  
  63. #include    <stdio.h>
  64. #include    <dos.h>
  65. #include    <ctype.h>
  66. #include    <assert.h>
  67. #include    <process.h>
  68.  
  69. /*
  70. **    site includes
  71. */
  72.  
  73. #include    <local/getargs.h>
  74. #include    <local/what.h>
  75.  
  76. #include    "idt.h"
  77.  
  78. #pragma    subtitle("Data and Definitions")
  79. #pragma    page()
  80.  
  81. /*
  82. **    global data
  83. */
  84.  
  85. IDT *idt;                /* address of internal data table    */
  86.  
  87. /*
  88. **    this cell is filled by our function rather that
  89. **    from the idt just so we can say we really did it.
  90. */
  91.  
  92. segment arenastart;            /* start of memory arena        */
  93.  
  94.  
  95. /*
  96. **    switch data
  97. */
  98.  
  99. static    int    doidt = 0;        /* list the internal data table        */
  100. static    int    dopdpt = 0;        /* list the physical device table    */
  101. static    int    doldt = 0;        /* list the logical device table    */
  102. static    int    dodevhdr = 0;        /* list device driver headers        */
  103. static    int    doarena = 0;        /* list memory arena            */
  104. static    int    dobcb = 0;        /* list buffer control blocks        */
  105. static    int    verbose = 0;        /* verbose listing (dumps buffers)    */
  106. static    int    doifcb = 0;        /* list internal file control blocks    */
  107. static    int    dofcbt = 0;        /* list file control blocks        */
  108. static    int    doall = 0;        /* list everything            */
  109.  
  110.  
  111. /*
  112. **    switch table
  113. */
  114.  
  115. ARG    swtab[] =
  116.     {
  117.     { 'i',    BOOLEAN,    &doidt,        "List internal data table"        },
  118.     { 'p',    BOOLEAN,    &dopdpt,    "List physical device table"        },
  119.     { 'l',    BOOLEAN,    &doldt,        "List logical device table"        },
  120.     { 'd',    BOOLEAN,    &dodevhdr,    "List device driver headers"        },
  121.     { 'm',    BOOLEAN,    &doarena,    "List memory arena"            },
  122.     { 'b',    BOOLEAN,    &dobcb,        "List buffer control blocks"        },
  123.     { 'f',    BOOLEAN,    &doifcb,    "List internal file control blocks"    },
  124.     { 'c',    BOOLEAN,    &dofcbt,    "List file control blocks"        },
  125.     { 'v',    BOOLEAN,    &verbose,    "Verbose (dump buffers)"        },
  126.     { '*',    BOOLEAN,    &doall,        "List everything"            }
  127.     };
  128.  
  129. #define    TABSIZE    (sizeof(swtab) / sizeof(ARG))
  130.  
  131.  
  132. /*
  133. **    prototypes
  134. */
  135.  
  136. int main( int, char ** );
  137.  
  138. segment findarena( void );
  139. segment isarena( segment, segment );
  140.  
  141. void showidt( IDT far * );
  142. void showpdpt( PDPT far * );
  143. void showpdpt1( PDPT far * );
  144. void showldt( LDT far * );
  145. void showdevhdr( DEVHDR far * );
  146. void showdevhdr1( DEVHDR far * );
  147. char *trimname( char far *, int );
  148. void showarena( MDB far * );
  149. int isenv( char far * );
  150. int ispsp( char far * );
  151. void showbcb( BCB far * );
  152. void dumpbuffer( byte far * );
  153. void paragraph( byte *, word );
  154. void memkpy( byte *, byte far *, int );
  155. int memkmp( byte *, byte far *, int );
  156. void showifcb( CHAIN far *, int );
  157. void showifcb1( IFCB far * );
  158.  
  159.  
  160.  
  161. /*
  162. **    "what" strings
  163. */
  164.  
  165. static char *WhatStrings[] =
  166.     {
  167.     WHATVER(PROGRAMNAME,VERSION,REVISION),
  168.     WHATWHEN,
  169.     WHATDATE,
  170.     WHAT("Copyright (c) 1988, Bill Parrott"),
  171.     WHAT("  All Rights Reserved")
  172.     };
  173.  
  174. #pragma    subtitle("main()")
  175. #pragma    page()
  176.  
  177. /*
  178. **    main  (need I say more?)
  179. */
  180.  
  181. int main( argc, argv )
  182.  
  183. int argc;
  184. char *argv[];
  185.  
  186.     {
  187.     union REGS r;
  188.     struct SREGS s;
  189.     IDT safeplaceforidt;
  190.     IDT far *p;
  191.     int i;
  192.  
  193.     /*
  194.     **    signon
  195.     */
  196.  
  197.     fprintf( stderr, "\n%s", &WhatStrings[0][4] );
  198.     fprintf( stderr, "\n%s\n", &WhatStrings[3][4] );
  199.  
  200.     /*
  201.     **    check for version
  202.     */
  203.  
  204.     r.h.ah = 0x30;
  205.     intdos( &r, &r );
  206.  
  207.     if ( r.h.al < 0 )
  208.         {
  209.         fprintf( stderr, "\nIncorrect version of MS-DOS.\n" );
  210.         return ( 1 );
  211.         }
  212.  
  213.  
  214.     /*
  215.     **    process the command line
  216.     */
  217.  
  218.     argc = getargs( argc, argv, swtab, TABSIZE );
  219.  
  220.     if ( doall )
  221.         doidt = dopdpt = doldt = dodevhdr = 
  222.         doarena = dobcb = doifcb = dofcbt = 1;
  223.     else
  224.         {
  225.         i  = doidt + dopdpt + doldt + dodevhdr +
  226.              doarena + dobcb + doifcb + dofcbt;
  227.  
  228.         if ( i == 0 )
  229.             {
  230.             fprintf( stderr, "\nusage: idt switch(es)\n" );
  231.             fprintf( stderr, "\n\t-i = list the internal data table" );
  232.             fprintf( stderr, "\n\t-p = list the physical device table" );
  233.             fprintf( stderr, "\n\t-l = list the logical device table" );
  234.             fprintf( stderr, "\n\t-d = list the device driver headers" );
  235.             fprintf( stderr, "\n\t-m = list the memory arena" );
  236.             fprintf( stderr, "\n\t-b = list buffer control blocks" );
  237.             fprintf( stderr, "\n\t-f = list internal file control blocks" );
  238.             fprintf( stderr, "\n\t-c = list file control blocks" );
  239.             fprintf( stderr, "\n\t-v = verbose (dump buffers)" );
  240.             fprintf( stderr, "\n\t-* = list all tables" );
  241.             fprintf( stderr, "\n" );
  242.             return ( 1 );
  243.             }
  244.         }
  245.  
  246.  
  247.     /*
  248.     **    find the memory arena on our own first.
  249.     */
  250.  
  251.     if ( (arenastart = findarena()) == 0 )
  252.         {
  253.         fprintf( stderr, "\n\n\a+++ Can't locate the first memory descriptor!! +++\n" );
  254.         return ( 1 );
  255.         }
  256.  
  257.  
  258.     /*
  259.     **    now look up the internal data table
  260.     */
  261.  
  262.     segread( &s );
  263.  
  264.     r.h.ah = 0x52;        /* magic undocumented function */
  265.     intdosx( &r, &r, &s );
  266.  
  267.  
  268.     /*
  269.     **    get a static copy for playing with
  270.     */
  271.  
  272.     idt = &safeplaceforidt;
  273.  
  274.     FP_SEG(p) = s.es;
  275.     FP_OFF(p) = r.x.bx - 8;
  276.  
  277.     memkpy( (byte *) idt, (byte far *) p, sizeof(IDT) );
  278.  
  279.  
  280.     /*
  281.     **    process selections
  282.     */
  283.  
  284.     if ( doidt )
  285.         showidt( idt );
  286.  
  287.     if ( dopdpt )
  288.         showpdpt( idt->pdpt );
  289.  
  290.     if ( doldt )
  291.         showldt( idt->ldt );
  292.  
  293.     if ( dodevhdr )
  294.         showdevhdr( &idt->nuldev );
  295.  
  296.     if ( doarena )
  297.         showarena( idt->arena );
  298.  
  299.     if ( dobcb )
  300.         showbcb( idt->bcbhead );
  301.  
  302.     if ( doifcb )
  303.         showifcb( idt->ifcb, 1 );
  304.  
  305.     if ( dofcbt )
  306.         showifcb( idt->fcbt, 0 );
  307.  
  308.  
  309.     /*
  310.     **    clean up
  311.     */
  312.  
  313.     printf( "\n" );
  314.  
  315.     return ( 0 );
  316.     }
  317.  
  318. #pragma subtitle("findarena() - find start of memory arena")
  319. #pragma    page()
  320.  
  321. /*
  322. **    function:    findarena
  323. **
  324. **    purpose:    This function demonstrates a well behaved (?)
  325. **            technique for finding the first memory control
  326. **            block.  It uses only information obtained from
  327. **            Microsoft literature and documents in this
  328. **            task... (hopefully freeing it from version
  329. **            dependancies.)  We do, however, make the
  330. **            assumption that MS-DOS memory descriptors
  331. **            are ALWAYS on paragraph boundaries.
  332. **
  333. **    usage:        first = findarena();
  334. **              segment first;
  335. **
  336. **    returns:    segment of first descriptor if found,
  337. **              else 0.
  338. */
  339.  
  340. segment findarena()
  341.  
  342.     {
  343.     word beginsearch;
  344.     word endsearch;
  345.     union REGS r;
  346.  
  347.     /*
  348.     **    find a place to give up the search.  For further
  349.     **    reliability, we make this the end of physical
  350.     **    memory (as DOS knows it).  Besides, the function
  351.     **    isarena() requires it.
  352.     */
  353.  
  354.     beginsearch = 0x40;        /* after interrupt table    */
  355.  
  356.     int86( 0x12, &r,